package promote.brozen.algorithm.leetcode._141;

/**
 * @author Brozen
 * @date 2020/9/5 2:34 PM
 * @email brozen@qq.com
 */
public class CircleList {
    class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
            next = null;
        }
    }

    public boolean hasCycle(ListNode head) {
        if (head == null) {
            return false;
        }

        ListNode fast = head;
        ListNode slow = head;

        do {
            slow = slow.next;
            fast = fast.next == null ? null : fast.next.next;
            if (fast == null || slow == null || fast.next == null) {
                return false;
            }
        } while(fast != slow);

        return true;
    }

}
